﻿/*******************************************************************
 *     JameE
 *******************************************************************
  Copyright 2010 Clifford Champion and JameE contributors.
  
  This file is part of JameE.
  
  JameE is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
  
  JameE is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
  
  You should have received a copy of the GNU General Public License
  along with JameE.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************/
 
 
 
/// <reference path="~/Base/Classer.js" />
/// <reference path="~/Base/IncludeLoader.js" />

$include(
[
	"Engine/SceneObject.js"
]);

$class(
"JameE.Assets.Objects.GraphVisualizer : SceneObject",
{
	/////////////////////
	// CONSTANTS
	/////////////


	/////////////////////
	// FIELDS
	/////////////
	_oGraph: null,
	_strVertexColor: null,
	_strEdgeColor: null,
	_fPointSize: null,


	/////////////////////
	// CONSTRUCTORS
	/////////////
	_ctor: function () {
		this._oGraph = $argument("graph", "Math.Graph") || null;
		this._strVertexColor = $argument("vertexColor", "string") || "#00EF00";
		this._strEdgeColor = $argument("edgeColor", "string") || "#EFEF00";
		this._fPointSize = $argument("pointSize", "number") || 2.0;
	},


	/////////////////////
	// METHODS
	/////////////
	setGraph: function (oGraph) {
		$require_type(oGraph, "JameE.Math.Graph");
		this._oGraph = oGraph;
	},


	/////////////////////
	// METHODS: SceneObject
	/////////////
	draw: function (oContext) {
		if (this._oGraph === null) {
			return;
		}

		oContext.save();
		oContext.beginPath();

		// prepare values
		var fPointSize = this._fPointSize;
		var fHalfPointSize = this._fPointSize / 2;

		// draw edges
		oContext.strokeStyle = this._strEdgeColor;
		oContext.lineWidth = this._fPointSize * 0.6666;
		this._oGraph.forEachEdge(function (iEdgeId, oVertexA, oVertexB) {
			oContext.beginPath();
			oContext.moveTo(oVertexA.fX, oVertexA.fY);
			oContext.lineTo(oVertexB.fX, oVertexB.fY);
			oContext.closePath();

			oContext.stroke();
		});

		// draw vertices
		oContext.fillStyle = this._strVertexColor;
		this._oGraph.forEachVertex(function (iVertexId, oVertex) {
			oContext.fillRect(
				oVertex.fX - fHalfPointSize,
				oVertex.fY - fHalfPointSize,
				fPointSize,
				fPointSize
			);
		});

		// done
		oContext.restore();
	}
});

$include_notify("Assets/Objects/GraphVisualizer.js");

